# coding: UTF-8
# @Date    ：2024/12/10 9:41 
# @File    : db_test.py
# :Author:  fum


import concurrent.futures

from api.fm.models.system.config_model import User

from tests.test_base import box

db_manager = box.db_manager()

# 单线程
def job():
    with db_manager.get_db_session() as session:
        users = []
        for i in range(1000000):
            new_user = User(username=f"User{i}", email=f"user{i}@example.com")
            users.append(new_user)
        session.bulk_save_objects(users)  # 一次性将所有用户添加到 session
        session.commit()  # 一次性提交所有数据
        print("All users added.")


# 多线程
with concurrent.futures.ThreadPoolExecutor() as executor:
    futures = [executor.submit(job) for _ in range(10)]
    for future in concurrent.futures.as_completed(futures):
        future.result()
    print("All jobs completed.")
